'use strict';

/**
 * @ngdoc filter
 * @name jgappApp.filter:dateFilter
 * @function
 * @description
 * # dateFilter
 * Filter in the jgappApp.
 */
angular.module('jgappApp')
    .filter('getDay', function () {
        return function (input) {
            if (input) {
                if (typeof input == "string") {
                    input = input.replace(/-/g, "/")
                }
                input = new Date(input)
                var weekday;
                switch (input.getDay()) {
                    case 1:
                        weekday = "一";
                        break;
                    case 2:
                        weekday = "二";
                        break;
                    case 3:
                        weekday = "三";
                        break;
                    case 4:
                        weekday = "四";
                        break;
                    case 5:
                        weekday = "五";
                        break;
                    case 6:
                        weekday = "六";
                        break;
                    case 0:
                        weekday = "天";
                        break;
                }
                return "星期" + weekday
            }
        };
    })
    .filter('timeFormat', function () {
        return function (time) {
            var date = "";
            if (typeof time != "undefined") {
                if (time < 10 && time >= 0) {
                    date = "0" + time
                } else {
                    date = date + time;
                }
            }
            return date;
        };
    })
    .filter('isCurDate', function ($filter) {
        return function (time, compare) {
            var isCurDate = false;
            if (typeof time != "undefined") {
                var date = new Date();
                if (typeof compare != "undefined") {
                    date = compare;
                }
                isCurDate = $filter("dateFmt")(time) == $filter("dateFmt")(date)
            }
            return isCurDate;
        };
    })
    .filter('dateFmt', function () {
        return function (date, fmt) {
            if (!date) {
                return
            }
            if (typeof date == "string") {
                date = date.replace(/-/g, "/")
            }
            date = new Date(date);
            fmt = fmt || "yyyy-MM-dd";
            var o = {
                "M+": date.getMonth() + 1, //月份 
                "d+": date.getDate(), //日 
                "h+": date.getHours(), //小时 
                "m+": date.getMinutes(), //分 
                "s+": date.getSeconds(), //秒 
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
                "S": date.getMilliseconds() //毫秒 
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        };
    });